package dm.api;

import org.apache.log4j.Logger;

import dm.bsi.Deployment;
import dm.bsi.Image;
import dm.bsi.Log;
import dm.bsi.MSDeployment;
import dm.bsi.WDeployment;
import indi.zjh.dp.access.Api;
import indi.zjh.dp.access.ApiContext;
import indi.zjh.dp.db.Configuration;
import indi.zjh.dp.db.Session;
import indi.zjh.dp.orm.Query;
import indi.zjh.dp.orm.Result;
import indi.zjh.dp.type.GSet;
import indi.zjh.dp.verify.CharSetRule;
import indi.zjh.dp.verify.Check;
import indi.zjh.dp.verify.RegularRule;
import indi.zjh.dp.verify.StringSetRule;
import indi.zjh.dp.verify.ValueRangeRule;

public class ScaleDeploymentApi extends Api {
	private static Logger logger = Logger.getLogger(ScaleDeploymentApi.class);
	
	@Override
	public void process(ApiContext ctx) throws Exception {
		// TODO Auto-generated method stub
		Session session = Configuration.getSessionFactory().createSession();
		try
		{
			String name = ctx.getRequestParamValue("name").trim();
			Check.data(name).desc("请输入合法存在的部署名称").rule(new CharSetRule(CharSetRule.NAME_SET));
			
			
			Query<Deployment> deployQuery = new Query<Deployment>(session);
			Result<Deployment> deployResult = deployQuery.find(Deployment.class, "`name` = '"+name+"'").result();
			if(deployResult.count() == 0)
			{
				throw new Exception("部署["+name+"]不存在");
			}
			
			Deployment deployment = deployResult.iterator().next();
			
			int mode = deployment.getMode();
			int workerCount = 0;
			int masterCount = 0;
			int slaveCount = 0;
			
			if(mode == Deployment.MODE_WORKER)
			{
				String workerCountStr = ctx.getRequestParamValue("replicas");
				Check.data(workerCountStr).desc("请输入容器副本个数replicas, 取值范围0-64").rule(new ValueRangeRule(0, 64));
				workerCount = Integer.parseInt(workerCountStr);
				
				Query<WDeployment> query = new Query<WDeployment>(session);
				Result<WDeployment> result = query.find(WDeployment.class).extend(WDeployment.class, Deployment.class, "`name` = '"+name+"'").result();
				if(result.count() == 0)
				{
					throw new Exception("部署["+name+"]不存在");
				}
				
				WDeployment wdeployment = result.iterator().next();
			
				wdeployment.setExpectedWokerCount(workerCount);
				session.save(wdeployment);
				Log.info(session, deployment, "设置部署容器个数 = "+workerCount);
			}
			else if(mode == Deployment.MODE_MASTER_SLAVE)
			{
				String replicasStr = ctx.getRequestParamValue("replicas");
				Check.data(replicasStr).desc("请输入master容器和slave容器副本个数, 以逗号隔开").rule(new RegularRule("\\d+,\\d+"));
				String[] strs = replicasStr.split(",");
				Check.data(strs[0]).desc("副本个数, 取值范围1-64").rule(new ValueRangeRule(0, 64));
				Check.data(strs[1]).desc("副本个数, 取值范围0-64").rule(new ValueRangeRule(0, 64));
				masterCount = Integer.parseInt(strs[0]);
				slaveCount = Integer.parseInt(strs[1]);
				
				
				Query<MSDeployment> query = new Query<MSDeployment>(session);
				Result<MSDeployment> result = query.find(MSDeployment.class).extend(MSDeployment.class, Deployment.class, "`name` = '"+name+"'").result();
				if(result.count() == 0)
				{
					throw new Exception("部署["+name+"]不存在");
				}
				
				MSDeployment msdeployment = result.iterator().next();
			
				msdeployment.setExpectedMasterCount(masterCount);
				msdeployment.setExpectedSlaveCount(slaveCount);
				session.save(msdeployment);
				Log.info(session, deployment, "设置部署容器master个数 = "+masterCount+", slave个数="+slaveCount);
			}
			
			
			session.commit();
			
			ctx.writeResponse("OK");
		}
		catch(Exception e)
		{
			ctx.writeResponse("FAIL:"+e.getMessage());
			session.rollback();
			e.printStackTrace();
		}
		finally {
			session.close();
		}
	}

}
